<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * ACC支付
 */
class AccController extends PayController{
    protected $config;
    protected $class;

    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data=[];
        $data['version'] = '1.0';
        $data['customerid'] = $this->config['mch_id'];
        $data['sdorderno'] = $return['orderid'];
        $data['total_fee'] = number_format($return['amount'],2,'.','');
        switch($this->config['paytype']) {
            //微信
            case 1:
                $data['paytype'] = 'bank_auto';
                break;
            case 3:
            case 4:
            case 17:
            case 18:
                $data['paytype'] = 'bank_auto';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        //$data['bankcode'] = '';
        $data['notifyurl'] = $return['notifyurl'];
        $data['returnurl'] = $return['callbackurl'];
        $data['remark'] = 'zf';
        //$data['get_code'] = '';

        $str = "version={$data['version']}&customerid={$data['customerid']}&total_fee={$data['total_fee']}&sdorderno={$data['sdorderno']}&notifyurl={$data['notifyurl']}&returnurl={$data['returnurl']}&{$this->config['signkey']}";
        $data['sign'] = md5($str);

        po($this->config['gateway'], $data);
    }

    //通知
    public function notifyurl(){
         //$this->debug_callback();
        if(!$this->checkip(getIP(),['43.229.38.130','43.229.38.132','103.197.69.81'])){
            exit("wrong");
        }

        $p = I("post.");

        //$p = array ( 'status' => '1', 'customerid' => '183', 'sdpayno' => '2019070504054694298', 'sdorderno' => 'sn20190705040545817', 'total_fee' => '1000.00', 'paytype' => 'acc_alipay', 'remark' => 'zf', 'realmoney' => '1000.00', 'sign' => 'f87c1a9427cf405adbb5d0e74bc815cf', );

        if(!$p || empty($p)){
            exit;
        }

        //获取订单信息
        $orderid = $p['sdorderno'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "pay_orderid_rd"=>htmlspecialchars($p['sdpayno']),
            'pay_true' => floatval($p['realmoney']),
        ));

        if( abs($p['total_fee'] - $p['realmoney']) > 1){
            echo 'money_not_match'; exit;
        }

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        if($p['respCode'] != 0){
            echo "pail_fail"; exit;
        }

        $sign = $p['sign'];
        unset($p['sign']);

        $str = "customerid={$p['customerid']}&status={$p['status']}&sdpayno={$p['sdpayno']}&sdorderno={$p['sdorderno']}&total_fee={$p['total_fee']}&paytype={$p['paytype']}&{$this->config['signkey']}";

        $sign2 = md5($str);

        if($sign !== $sign2){
            exit("fail");
        }

        if($p['status'] !== '1'){
            echo 'pay fail'; exit;
        }

        $this->async($find['pay_orderid'], 'success');
    }

    public function callBackUrl(){

        echo '操作成功'; exit;
    }
}